package com.study.webapp.leetcode;


import java.util.PriorityQueue;

/**
 * 数据流中的第K大元素
 */
public class Leet_703 {


    public static void main(String[] args) {
        int[] a = new int[] {2,3,4,6,10,8};
        KthLargest kthLargest = new KthLargest(3,a);
        System.out.println(kthLargest.add(5));
    }




     static class KthLargest {
        final PriorityQueue<Integer> queue;
        final int k;

        public KthLargest (int k, int[] nums) {
            this.k = k;
            queue = new PriorityQueue<>(k);
            for (int a : nums) {
                add(a);
            }
        }

        public int add(int a) {
            if (queue.size() < k) {
                queue.offer(a);
            } else if (queue.peek() < a) {
                queue.poll();
                queue.offer(a);
            }
            return queue.peek();
        }
    }
}
